﻿using System;
using System.Windows.Input;
using Gencode.VECore.VectorShape;

namespace Gencode.VECore.Tools
{
	public class RectangleTool : FactoryTool
	{
        protected override VECShape CreateShape(System.Windows.Point mousePosition)
        {
            var shape = new VECRectangle(mousePosition, new System.Windows.Size(1, 1));

            shape.Fill = ToolProperty.Fill;
            shape.StrokeThickness = ToolProperty.StrokeThickness;
            shape.Stroke = ToolProperty.Stroke;

            return shape;
        }

        public override void OnMouseLeftButtonUp(CanvasDocument sender, System.Windows.Input.MouseButtonEventArgs e)
		{
            //Add object to history
            var shape = sender.SelectedLayer.ShapeList[0] as VECRectangle;
            if (shape != null)
                sender.AddCommandToHistory(new Gencode.VECore.History.AddCommand(shape));
		}

        public override void OnMouseMove(CanvasDocument sender, System.Windows.Input.MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
                var shape = sender.SelectedLayer.ShapeList[0] as VECRectangle;
                if (shape != null)// CanvasManager.SelectedObject != null)
                {
                    System.Windows.Point m = Mouse.GetPosition(sender);
                    //var rect = CanvasManager.SelectedObject as VECRectangle;

                    if (m.Y > (shape.Y + (shape.Height / 2)) &&
                        m.X > (shape.X + (shape.Width / 2)))//lower right
                        shape.MoveHandle(m, 3);
                    else if (m.Y > (shape.Y + (shape.Height / 2)) &&
                        m.X < (shape.X + (shape.Width / 2)))//lower left
                        shape.MoveHandle(m, 4);
                    else if (m.Y < (shape.Y + (shape.Height / 2)) &&
                        m.X < (shape.X + (shape.Width / 2)))//upper left
                        shape.MoveHandle(m, 1);
                    else//upper right
                        shape.MoveHandle(m, 2);
                }
			}
		}
	}
}
